#ifndef LEAF_H
#define LEAF_H

#include <stdint.h>
#include <stddef.h>
// DNS查询状态
typedef enum {
    DNS_QUERY_IN_PROGRESS,
    DNS_QUERY_SUCCESS,
    DNS_QUERY_FAILED
} dns_query_status_t;
// 网关描述结构体
struct Gateway {
    const char* host;
    int port;
    uint32_t weight; // 权重值
    uint32_t latency; // 动态延迟(ms)
};
// HTTP会话签注结构
typedef struct {
    uint32_t session_id;
    char client_ip[16];
    char server_ip[16];
    uint16_t client_port;
    uint16_t server_port;
    char method[8];
    char host[256];
    char uri[512];
    uint64_t timestamp;
    uint32_t signature; // 签注值
} HttpSessionStamp;
typedef void (*write_packet_t)(const uint8_t* data, size_t len);
// DNS解析回调
typedef void (*dns_resolve_cb_t)(const char* domain, uint32_t ip, void* userdata);
// 网关状态变更回调
typedef void (*gateway_change_cb_t)(const char* host, int port, uint32_t latency, void* userdata);
// HTTP回调函数类型
typedef void (*http_session_cb_t)(const HttpSessionStamp* stamp, void* userdata);
// Protocol回调函数类型
typedef void (*packet_callback_t)(const uint8_t* data, size_t len, void* userdata);

extern "C" {
    void input_packet(const uint8_t* data, size_t len);
    bool start_socks(write_packet_t write_packet, const char *proxy_host, int proxy_port);
    int get_packet_protocol(const uint8_t* data, size_t len);
    void set_packet_callback(packet_callback_t cb, void* userdata);
    // DNS功能API
    void dns_set_local(const char* domain, uint32_t ip); // 设置本地域名解析
    uint32_t dns_resolve_local(const char* domain);      // 本地域名解析
    void dns_set_upstream(const char* dns_server);       // 设置上游DNS
    void dns_set_callback(dns_resolve_cb_t cb, void* userdata); // DNS回调设置
    // 多网关管理
    void gateway_add(const Gateway* gw); 
    void gateway_remove(const char* host);
    Gateway* gateway_select(); // 基于权重和延迟的选择
    void gateway_set_callback(gateway_change_cb_t cb, void* userdata); // 网关回调设置
    void gateway_update_latency(const char* host, uint32_t latency); // 更新网关延迟
    // 资源寻址
    void resource_register(const char* uri, const char* gateway_group);
    const char* resource_locate(const char* uri); // 返回最优网关组
    // HTTP会话管理
    void http_set_session_callback(http_session_cb_t cb, void* userdata);
    void http_enable_protocol_analysis(bool enable);
    uint32_t http_generate_signature(const char* data, size_t len);
}

#endif  // LEAF_H
